package com.launcher.wkang.fragment;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.launcher.wkang.App;
import com.launcher.wkang.R;

import com.launcher.wkang.activity.IntermediaryActivity;
import com.launcher.wkang.activity.LauncherActivity;
import com.launcher.wkang.activity.LauncherActivity1;
import com.launcher.wkang.activity.StudentSettingActivity;
import com.launcher.wkang.adapter.ApplicationAdapter;
import com.launcher.wkang.bean.AppInfo;


import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class Fragment4 extends Fragment implements AdapterView.OnItemClickListener,  AdapterView.OnItemLongClickListener, View.OnClickListener {
    private List<ResolveInfo> mApps;
    private GridView gridView;
    private ApplicationAdapter applicationAdapter;
    private View view;
    List<AppInfo> list;
    private LinearLayout desktopAnagementLayout;
    private LinearLayout characteristicFunctionLayout;
    private LinearLayout rootLayout;
    private final static int DESKTOPINTENT=1001;

    private final static int CHARACTERISTICFUNCTION=1002;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view =getActivity().getLayoutInflater().inflate(R.layout.fragment_layout4,null);
        initView();
        initData();
        return view;
    }
    public GridView getGridView(){
        return gridView;
    }
    public LinearLayout getRootLayout(){
        return rootLayout;
    }

    @Override
    public void onResume() {
        super.onResume();
        App.getINstance().setsetWaveformMode(5,1);
    }

    private void initView(){
        gridView=(GridView) view.findViewById(R.id.application_list);
        desktopAnagementLayout=view.findViewById(R.id.desktop_anagement_layout);
        desktopAnagementLayout.setOnClickListener(this);
        characteristicFunctionLayout=view.findViewById(R.id.characteristic_function_layout);
        characteristicFunctionLayout.setOnClickListener(this);
        rootLayout=view.findViewById(R.id.root_Layout);

    }
    private void initData(){

        applicationAdapter=new ApplicationAdapter(getActivity());
        gridView.setAdapter(applicationAdapter);
        gridView.setOnItemClickListener(this);
//        gridView.setOnItemLongClickListener(this);
        loadApps();

    }

    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        mApps = getActivity().getPackageManager().queryIntentActivities(mainIntent, 0);
        for (int i=0;i<mApps.size();i++){
            ResolveInfo resolveInfo=mApps.get(i);
            Log.d("Fragment4","package="+resolveInfo.activityInfo.packageName+"activity="+resolveInfo.activityInfo.name);
        }


        list=new ArrayList<AppInfo>();
//
        AppInfo appInfo1=new AppInfo();
        appInfo1.setAppName("音乐");
        appInfo1.setAppClass("com.android.music.MusicBrowserActivity");
        appInfo1.setAppPackage("com.android.music");
        appInfo1.setAppIcon(R.mipmap.music);
        list.add(appInfo1);

        AppInfo appInfo2=new AppInfo();
        appInfo2.setAppName("视频");
        appInfo2.setAppClass("com.google.android.youtube.videos.EntryPoint");
        appInfo2.setAppPackage("com.google.android.videos");
        appInfo2.setAppIcon(R.mipmap.video);
        list.add(appInfo2);

        AppInfo appInfo3=new AppInfo();
        appInfo3.setAppName("文件管理");
        appInfo3.setAppClass("com.mediatek.filemanager.FileManagerOperationActivity");
        appInfo3.setAppPackage("com.mediatek.filemanager");
        appInfo3.setAppIcon(R.mipmap.file_management);
        list.add(appInfo3);

        AppInfo appInfo4=new AppInfo();
        appInfo4.setAppName("时钟");
        appInfo4.setAppClass("com.android.deskclock.DeskClock");
        appInfo4.setAppPackage("com.android.deskclock");
        appInfo4.setAppIcon(R.mipmap.clock);
        list.add(appInfo4);

        AppInfo appInfo5=new AppInfo();
        appInfo5.setAppName("日历");
        appInfo5.setAppClass("com.android.calendar.AllInOneActivity");
        appInfo5.setAppPackage("com.android.calendar");
        appInfo5.setAppIcon(R.mipmap.calendar);
        list.add(appInfo5);

        AppInfo appInfo6=new AppInfo();
        appInfo6.setAppName("相册");
        appInfo6.setAppClass("com.android.gallery3d.app.GalleryActivity");
        appInfo6.setAppPackage("com.android.gallery3d");
        appInfo6.setAppIcon(R.mipmap.album);
        list.add(appInfo6);
        applicationAdapter.initData(mApps);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final  int position, long id) {
        ResolveInfo info= (ResolveInfo) parent.getItemAtPosition(position);
//        ResolveInfo info = mApps.get(position);
        //该应用的包名
        String pkg = info.activityInfo.packageName;
        //应用的主activity类
        String cls = info.activityInfo.name;
        Intent intent=new Intent(getActivity(),IntermediaryActivity.class);

        intent.putExtra("pkg",pkg);
        intent.putExtra("cls",cls);
        startActivity(intent);

    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Intent uninstall_intent = new Intent(); uninstall_intent.setAction(Intent.ACTION_DELETE);
        ResolveInfo info = mApps.get(position);
        //该应用的包名
        String pkg = info.activityInfo.packageName;
        uninstall_intent.setData(Uri.parse("package:"+pkg)); startActivity(uninstall_intent);
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.desktop_anagement_layout:
                App.getINstance().setsetWaveformMode(2,1);
                Intent deskTopIntent=new Intent(getActivity(), StudentSettingActivity.class);
                startActivity(deskTopIntent);


                break;
            case R.id.characteristic_function_layout:
                Intent intent=new Intent(getActivity(),IntermediaryActivity.class);

                intent.putExtra("pkg","com.android.settings");
                intent.putExtra("cls","com.android.settings.Settings");
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            if (requestCode==DESKTOPINTENT||requestCode==CHARACTERISTICFUNCTION){
                int result = data.getExtras().getInt("result");
//                ((LauncherActivity1)getActivity()).setCurrentItem(result);
            }
        }
    }
    @Override
    public void onPause() {
        super.onPause();
//        App.getINstance().setsetWaveformMode(2,1);
    }

}
